//
//  LCWEmotionTools.swift
//  MyWB
//
//  Created by 李成万 on 16/7/5.
//  Copyright © 2016年 李成万. All rights reserved.
//

import UIKit

let EmotionPageViewCol = 7
let EmotionPageViewrow = 3
let EmotionPageViewCount = EmotionPageViewCol * EmotionPageViewrow - 1

class LCWEmotionTools: NSObject {

    static let sharedTools : LCWEmotionTools = LCWEmotionTools()
    
    // MARK: - 一维数组们

    lazy var defaultEmotion : [LCWEmotions] = {
        return self.getEmotion("default/")
    }()
    
    lazy var emojiEmotion : [LCWEmotions] = {
       return self.getEmotion("emoji/")
    }()
    
    lazy var lxhEmotion : [LCWEmotions] = {
        return self.getEmotion("lxh/")
    }()
    
    // MARK: - 一个 bundle 路径
    lazy var emotionBundle : NSBundle = {
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)
        
        return NSBundle(path: path ?? "")!
    }()

    
    // MARK: - 三维数组
    lazy var emotionss : [[[LCWEmotions]]] = {
       return [
        self.getEmotions(self.defaultEmotion),
        self.getEmotions(self.emojiEmotion),
        self.getEmotions(self.lxhEmotion)
        ]
    }()
    

    func searchEmotion(chs:String) -> LCWEmotions?{
        
        for e in defaultEmotion{
            if e.chs == chs{
                return e
            }
        }
        
        for e in lxhEmotion{
            if e.chs == chs{
                return e
            }
        }
        return nil
        
    }
//    override init() {
//        super.init()
//        getEmotion()
//    }
//    /Users/lichengwan/Desktop/MyWeibo/MyWeibo/MyWB/MyWB/Classes/Compose/Views/EmotionView/Emoticons.bundle/Contents/Resources/emoji/info.plist
    
    /**
     获取表情保存在一维数组
     */
    func getEmotion(fileName:String) -> [LCWEmotions]{
//        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)
        //        print(path):"/Users/lichengwan/Library/Developer/CoreSimulator/Devices/44261E1C-D5DA-4BBC-BC8E-8360AAE6D409/data/Containers/Bundle/Application/929F110E-BD43-4ABB-BADE-AA6454BEF8E7/MyWB.app/Emoticons.bundle"
        
        let file = emotionBundle.pathForResource("\(fileName)info.plist", ofType: nil)
        
        //+emoji/ :"/Users/lichengwan/Library/Developer/CoreSimulator/Devices/44261E1C-D5DA-4BBC-BC8E-8360AAE6D409/data/Containers/Bundle/Application/9B1D43EF-D65F-4E24-BC9D-3774000B408E/MyWB.app/Emoticons.bundle/Contents/Resources/emoji/info.plist"
//        print(file)
        
        let emoArr = NSArray(contentsOfFile: file ?? "")!
        
        var tempArr : [LCWEmotions] = [LCWEmotions]()
        for obj in emoArr{
            let emotion = LCWEmotions(dict: obj as! [String : AnyObject])
            
            emotion.path = "\(fileName)"
            
            tempArr.append(emotion)
        }
        
        return tempArr
    }
    
    /**
     获取二维数组
     */
    func getEmotions(emotion:[LCWEmotions]) -> [[LCWEmotions]]{
        
        let pageCount = (emotion.count + EmotionPageViewCount - 1) / EmotionPageViewCount
        
//        var pageCount = emotion.count / EmotionPageViewCount
//        if emotion.count % EmotionPageViewCount != 0 {
//            pageCount += 1
//        }

        var emotions : [[LCWEmotions]] = [[LCWEmotions]]()
        for i in 0..<pageCount{
            let loc = EmotionPageViewCount * i
            var len = EmotionPageViewCount
             
            if loc + len > emotion.count{
                len = emotion.count - loc
            }
            
            
            emotions.append((emotion as NSArray).subarrayWithRange(NSMakeRange(loc, len)) as! [LCWEmotions])
        }
        return emotions
    }
    
    
}
